﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Omu.AwesomeDemo.Core.Model;
using Omu.AwesomeDemo.Core.Repository;

namespace Omu.AwesomeDemo.WebUI.Areas.Veggies.Controllers
{
    public class FruitsAjaxCheckBoxListController : Controller
    {
        private readonly IRepo<Fruit> r;

        public FruitsAjaxCheckBoxListController(IRepo<Fruit> r)
        {
            this.r = r;
        }

        public ActionResult GetItems(IEnumerable<int> keys)
        {
            keys = keys ?? new List<int>();
            var res = r.GetAll().Select(o =>
                                        new SelectListItem
                                            {
                                                Text = o.Name,
                                                Value = o.Id.ToString(),
                                                Selected = (keys.Contains(o.Id))
                                            });

            return Json(res);
        }
    }
}